// ArtWizardSolution.h : Declaration of the CArtWizardSolution

#pragma once
#include "resource.h"       // main symbols

#include "ArtHelper.h"
#include "_IArtWizardSolutionEvents_CP.h"
#include "WizardBaseClass.h"

#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."
#endif


class CCustomParamsIniFile : public saf::CIniFile
{
private:
	ATL::CAtlFile af;

#define ART_KEY_PARAM	_T("ArtParam")

	BOOL CreateIniFile()
	{
		CPath		pathTemp;
		CString		strFullName;

		GetModuleFileName(_AtlBaseModule.GetModuleInstance(), (LPTSTR)pathTemp.m_strPath.GetBuffer(MAX_PATH), MAX_PATH);
		pathTemp.m_strPath.ReleaseBuffer();
		pathTemp.RemoveFileSpec();

		if (pathTemp.m_strPath.IsEmpty())
		{
			if (GetTempPath(MAX_PATH, pathTemp.m_strPath.GetBuffer(MAX_PATH)) <= 0)
				pathTemp = _T("D:");
		}

		if (pathTemp.m_strPath[pathTemp.m_strPath.GetLength()-1] == '\\')
		{
			strFullName.Format(_T("%sArtWizard.ini"), (LPCTSTR)pathTemp.m_strPath);
		}else
		{
			strFullName.Format(_T("%s\\ArtWizard.ini"), (LPCTSTR)pathTemp.m_strPath);
		}

		if (PathFileExists(strFullName))
		{
			DeleteFile(strFullName);
		}

		if (S_OK  == af.Create((LPCTSTR)strFullName, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, CREATE_NEW, 
			FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN) )
		{
			this->SetFileName(strFullName);
			CString strKey;
			strKey.Format(_T("[%s]"), ART_KEY_PARAM);
			WriteLine(strKey);
			return TRUE;
		}

		ATLASSERT (0 && "Create %S Error.");
		return FALSE;
	}

public:
	CCustomParamsIniFile()
	{
		CreateIniFile();
	}
	~CCustomParamsIniFile()
	{
		af.Close();
	}

	BOOL WriteLine(LPCTSTR lpszInfo)
	{
		CString strText(lpszInfo);
		return WriteLine(strText);
	}

	BOOL WriteLine(CString &strItem)
	{
		if (!strItem.IsEmpty())
		{
			strItem.AppendChar('\n');
			if (S_OK == af.Write((void*)(LPCTSTR)strItem, strItem.GetLength()*sizeof(TCHAR)) )
				return TRUE;
		}

		return FALSE;
	}

	BOOL CustomParamsToIniFile(SAFEARRAY *CustomParams)
	{
		ATL::CComSafeArray<VARIANT>		safeArray(CustomParams);
		ULONG			nCount = safeArray.GetCount();
		CComVariant		varItem;
		CString			strItem;
		ULONG				nIndex = 0;

		for (; nIndex<nCount; nIndex++)
		{
			varItem = safeArray.GetAt(nIndex);
			strItem = varItem;
			WriteLine(strItem);
		}

		if (nIndex > 0)
			return TRUE;

		return FALSE;
	}
};


#define ART_PROJECT_TYPE	_T("ART_PROJECT_TYPE")
#define ART_MFC_BODY		_T("ART_MFC_BODY")
#define ART_MFC_APP			_T("ART_MFC_APP")

#define ART_ATL_BODY		_T("ART_ATL_BODY")
#define ART_ATL_APP			_T("ART_ATL_APP")

#define ART_WTL_BODY		_T("ART_WTL_BODY")
#define ART_WTL_APP			_T("ART_WTL_APP")
#define ART_LANGUAGE_DLL	_T("ART_LANGUAGE_DLL")


// CArtWizardSolution

class ATL_NO_VTABLE CArtWizardSolution :
	public CComObjectRootEx<CComSingleThreadModel>,
	public CComCoClass<CArtWizardSolution, &CLSID_ArtWizardSolution>,
	public ISupportErrorInfo,
	public IConnectionPointContainerImpl<CArtWizardSolution>,
	public CProxy_IArtWizardSolutionEvents<CArtWizardSolution>,
	public IDispatchImpl<IArtWizardSolution, &IID_IArtWizardSolution, &LIBID_ArtHelperLib, /*wMajor =*/ 1, /*wMinor =*/ 0>,
	public IDispatchImpl<EnvDTE::IDTWizard, &__uuidof(EnvDTE::IDTWizard), &EnvDTE::LIBID_EnvDTE, /* wMajor = */ 8>
{
private:

	HRESULT CreateLanguageProject(EnvDTE80::Solution2 *pSolution2);

	BOOL Init(void);
	void UnInit(void);
	artWizardType CreateTypeObject( CString &strType );

	HRESULT CreateTypeObjectError();
	artWizardType CreateTypeObjectATL( CString &strType);
	artWizardType CreateTypeObject_MFC( CString &strType );

public:
	//HWND			 	m_hwndOwner;

	static CArtWizardSolution	*m_pWizardSolution;
	static CArtWizardSolution *Instace()				{ return m_pWizardSolution;	}
	BOOL QuitSolutionState(void);
	std::auto_ptr<CWizardBaseClass> m_pWizard;

	// system com
public:
	CArtWizardSolution();
	~CArtWizardSolution();

	DECLARE_REGISTRY_RESOURCEID(IDR_ARTWIZARDSOLUTION)


	BEGIN_COM_MAP(CArtWizardSolution)
		COM_INTERFACE_ENTRY(IArtWizardSolution)
		COM_INTERFACE_ENTRY2(IDispatch, IDTWizard)
		COM_INTERFACE_ENTRY(ISupportErrorInfo)
		COM_INTERFACE_ENTRY(IConnectionPointContainer)
		COM_INTERFACE_ENTRY(IDTWizard)
	END_COM_MAP()

	BEGIN_CONNECTION_POINT_MAP(CArtWizardSolution)
		CONNECTION_POINT_ENTRY(__uuidof(_IArtWizardSolutionEvents))
	END_CONNECTION_POINT_MAP()
	// ISupportsErrorInfo
	STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);


	DECLARE_PROTECT_FINAL_CONSTRUCT()

	HRESULT FinalConstruct()
	{
		Init();
		return S_OK;
	}

	void FinalRelease()
	{
	//	UnInit();
	}

public:

	STDMETHOD(Execute)(LPDISPATCH Application, long hwndOwner, SAFEARRAY * * ContextParams, SAFEARRAY * * CustomParams, EnvDTE::wizardResult * retval);
	void DataViewClassToView(void);
};


OBJECT_ENTRY_AUTO(__uuidof(ArtWizardSolution), CArtWizardSolution)

//extern CArtWizardSolution *gpWizardSolution;
